require("./console");
var appUtils = require("../utils/appUtils");
var timeUtils = require("../utils/timeUtils");
var randomUtils = require("../utils/randomUtils");
var logUtils = require("../utils/logUtils");
function App(name, config) {
  this.name = name;
  this.storage = storages.create(timeUtils.str() + this.name);
  this.maxAllowRunHour = config.maxAllowRunHour;
  this.minAllowRunHour = config.minAllowRunHour;
  this.maxRunSCount = config.maxRunSCount;
  this.maxRunFCount = config.maxRunFCount;
  this.interval = config.interval;
  this.installed = null;
  this.todayIsOver = false;
  Object.defineProperty(this, "runSCount", {
    get() {
      return this.storage.get(this.name + "runSCount") || 0;
    },
    set(value) {
      this.storage.put(this.name + "runSCount", value);
    },
  });
  Object.defineProperty(this, "runFCount", {
    get() {
      return this.storage.get(this.name + "runFCount") || 0;
    },
    set(value) {
      this.storage.put(this.name + "runFCount", value);
    },
  });
}
App.prototype.main = function () {
  if (this.canRun()) {
    logUtils.log(this.name + "开始运行");
    this.open();
    this.lastStartTime = Date.now();
    sleep(randomUtils.getRandom(6, 8, 1000));
    this.run();
    this.close();
    this.lastEndTime = Date.now();
    logUtils.log(this.name + "结束运行");
  }
};
App.prototype.open = function () {
  return appUtils.open(this.name);
};
App.prototype.close = function () {
  return appUtils.closeRecent(this.name);
};
App.prototype.canRun = function () {
  if (!this.checkInstalled()) {
    return;
  }
  if (!this.checkAllowRunHour()) {
    return;
  }
  if (!this.checkRunCount()) {
    return;
  }
  if (!this.checkInterval()) {
    return;
  }

  if (this.todayIsOver) {
    return;
  }
  return true;
};
App.prototype.run = function () {};
App.prototype.checkInstalled = function () {
  if (this.installed === null) {
    this.installed = appUtils.checkInstalled(this.name);
    if (!this.installed) {
      console.log("checkInstalled: " + this.name + " 未安装");
    }
  }
  return this.installed;
};
App.prototype.checkInterval = function () {
  if (this.interval && this.lastEndTime) {
    var hasExec = timeUtils.hasExec(this.lastEndTime);
    if (hasExec < this.interval) {
      console.log(this.name, "间隔时间未到", hasExec, "/", this.interval);
      return false;
    }
  }
  return true;
};
App.prototype.checkAllowRunHour = function () {
  var hour = new Date().getHours();
  if (this.maxAllowRunHour) {
    if (hour >= this.maxAllowRunHour) {
      console.log(
        this.name,
        hour,
        "|",
        maxAllowRunHour,
        "最大允许执行时间已过"
      );
      return false;
    }
  }
  if (this.minAllowRunHour) {
    if (hour < this.minAllowRunHour) {
      console.log(
        this.name,
        hour,
        "|",
        minAllowRunHour,
        "最小允许执行时间未到"
      );
      return false;
    }
  }
  return true;
};
App.prototype.checkRunCount = function () {
  if (this.maxRunSCount) {
    if (this.runSCount >= this.maxRunSCount) {
      console.log(
        this.name,
        this.runSCount,
        "|",
        this.maxRunSCount,
        "执行成功次数超过限制"
      );
      return false;
    }
  }
  if (this.maxRunFCount) {
    if (this.runFCount >= this.maxRunFCount) {
      console.log(
        this.name,
        this.runFCount,
        "|",
        this.maxRunFCount,
        "执行失败次数超过限制"
      );
      return false;
    }
  }
  return true;
};
module.exports = App;
